package com.zhongli.devplatform.security.qq;

import com.zhongli.devplatform.component.qq.QQClientConfig;
import com.zhongli.devplatform.component.qq.QQAuthUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义qq登录认证过滤器
 * @author WuXiCheng
 */
@Slf4j
public class QQAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {

    @Autowired
    private QQClientConfig clientConfig;

    @Autowired
    private QQAuthUtil authUtil;

    public QQAuthenticationProcessingFilter(String defaultFilterProcessesUrl) {
        super(new AntPathRequestMatcher(defaultFilterProcessesUrl, "GET"));
    }

    @SneakyThrows
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        UsernamePasswordAuthenticationToken authenticationToken =  null;
        //获取请求code
        String code = request.getParameter(clientConfig.getResponse_type());
        //拼接获取tokenUrl
        String tokenAccessApi = String.format(clientConfig.getToken_access_api(), clientConfig.getAccessTokenUri(), clientConfig.getGrantType(), clientConfig.getAppId(), clientConfig.getSecret(), code, clientConfig.getRedirectUri());
        //获取qq认证后accessToken
        QQAccessToken qqAccessToken = authUtil.getToken(tokenAccessApi);
        if (null != qqAccessToken){
            String openId = authUtil.getOpenId(qqAccessToken.getAccessToken());
//            System.out.println("输出openId:" + openId);
            if (null != openId){
                authenticationToken = new UsernamePasswordAuthenticationToken(qqAccessToken.getAccessToken(),openId);
                //TODO 具体是否注册用户业务待完成
            }
        }
//        if (qqAccessToken != null){
//            //请求openId
//            String openId = getOpenId(qqAccessToken.getAccessToken());
//            if (openId != null){
//                try {
//                    // 生成验证 authenticationToken
//                    authenticationToken = new UsernamePasswordAuthenticationToken(qqAccessToken.getAccessToken(), openId);
//                    QQUserInfo userInfo = getUserInfo(qqAccessToken.getAccessToken(), openId);
//                    QQRegisterService qqRegisterService = SpringUtil.getBean(QQRegisterService.class);
//                    qqRegisterService.register(userInfo);
//                } catch (Exception e) {
//                    log.error("qq自动注册失败：" + e.getMessage());
//                    e.printStackTrace();
//                }
//            }
//        }
        // 返回验证结果
        return this.getAuthenticationManager().authenticate(authenticationToken);
    }
}